package com.cloudlead.userAction.Service.impl;

import com.cloudlead.common.worker.client.exception.RestClientException;
import com.cloudlead.commonData.service.BaseCompanyUserService;
import com.cloudlead.commonData.vo.*;
import com.cloudlead.plugin.security.service.ScUserService;
import com.cloudlead.plugin.security.vo.ScUser;
import com.cloudlead.userAction.Service.BaseCompanySer;;
import  com.cloudlead.commonData.service.BaseCompanyService;
import com.cloudlead.userService.vo.BaseCompanyVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by Administrator on 2015-08-13.
 */
@Service
public class BaseCompanySerImpl implements BaseCompanySer {
    @Autowired
    BaseCompanyService baseCompanyService;

    @Autowired
    BaseCompanyUserService baseCompanyUserService;

    @Autowired
    ScUserService scUserService;

    @Override
    @Transactional//事务回滚
    public String companyRegister(BaseCompanyVO baseCompanyVO) {

        BaseCompany baseCompany=new BaseCompany();
        BaseCompanyUser baseCompanyUser=new BaseCompanyUser();
        baseCompanyUser.setUserName(baseCompanyVO.getUserName());
        baseCompany.setCompanyName(baseCompanyVO.getCompanyName());
        if(baseCompanyUserService.findOne(baseCompanyUser)!=null){
            throw new RestClientException("用户\""+baseCompanyVO.getUserName()+"\"已存在。");
        }
        if(baseCompanyService.findOne(baseCompany)!=null){
            throw new RestClientException("公司\""+baseCompanyVO.getCompanyName()+"\"已存在。");
        }
        baseCompany.setFlag("0");
        baseCompanyUser.setFlag("0");
        baseCompanyUser.setIsAdmin(1);//超级用户

        baseCompanyUser.setUserName(baseCompanyVO.getUserName());//用户名
        baseCompanyUser.setName(baseCompanyVO.getUserName());//姓名
        baseCompanyUser.setPassword(baseCompanyVO.getPassWord());//密码
        baseCompanyUser.seteMail(baseCompanyVO.geteMail());//邮箱
        baseCompanyUser.setPhone(baseCompanyVO.getPhnoe());//电话
        baseCompanyUser.setStatus("0");

        baseCompany.setFleetArea(baseCompanyVO.getFleetArea());//所属区域
        baseCompany.setAccountName(baseCompanyVO.getAccountName());//企业银行账户
        baseCompany.setIdentity(baseCompanyVO.getIdentity());//身份
        baseCompany.setOrganizationCode(baseCompanyVO.getOrganizationCode());//组织机构代码
        baseCompany.setRegistrationNumber(baseCompanyVO.getRegistrationNumber());//工商注册号
        baseCompany.setCarsCount(baseCompanyVO.getCarsCount());//车辆总数
        baseCompany.setCompanyName(baseCompanyVO.getCompanyName());//公司名称
        baseCompany.setBank(baseCompanyVO.getBank());//开户银行
        baseCompany.setComments(baseCompanyVO.getComments());//备注
        baseCompany.setBusinessLicense(baseCompanyVO.getBusinessLicense());//营业执照
        baseCompany.setLogoImage(baseCompanyVO.getLogoImage());//logo图标
        baseCompany.setTaxRegistrationFilePath(baseCompanyVO.getTaxRegistrationFilePath());//税务登记证
        baseCompany.setCompanyPhone1(baseCompanyVO.getCompanyPhone1());
        baseCompany.setCompanyPhone2(baseCompanyVO.getCompanyPhone2());
        baseCompany.setStatus("0");


        baseCompanyService.create(baseCompany);
        baseCompanyUser.setBaseCompany(baseCompany);
        baseCompanyUserService.create(baseCompanyUser);

        return "注册成功";
    }

    @Override
    @Transactional//事务回滚
    public void companyUpdate(BaseCompanyVO baseCompanyVO) {
        BaseCompany baseCompany = baseCompanyService.view(baseCompanyVO.getId());
        if(baseCompany!=null){
            //用户
            BaseCompanyUser baseCompanyUser = new BaseCompanyUser();
            baseCompanyUser.setBaseCompany(baseCompany);
            BaseCompanyUser user = baseCompanyUserService.findOne(baseCompanyUser);
            if(baseCompanyVO.getPhnoe()!=null&&!baseCompanyVO.getPhnoe().equals("")) {//手机不为空，则执行
                user.setPhone(baseCompanyVO.getPhnoe());//手机号
            }
            if(baseCompanyVO.geteMail()!=null&&!baseCompanyVO.geteMail().equals("")) {//邮箱不为空，则执行
                user.seteMail(baseCompanyVO.geteMail());//邮箱
            }
            baseCompanyUserService.update(user);//编辑用户

            //权限用户
            ScUser scUser = new ScUser();
            scUser.setUsername(user.getUserName());
            ScUser scUser1 = scUserService.findOne(scUser);
            if(baseCompanyVO.getPhnoe()!=null&&!baseCompanyVO.getPhnoe().equals("")) {//手机不为空，则执行
                scUser1.setMobile(baseCompanyVO.getPhnoe());//手机号
            }
            if(baseCompanyVO.geteMail()!=null&&!baseCompanyVO.geteMail().equals("")) {//邮箱不为空，则执行
                scUser1.setEmail(baseCompanyVO.geteMail());//邮箱
            }
            scUserService.update(scUser1);//编辑权限用户
        }
        //公司
        baseCompany.setCompanyPhone1(baseCompanyVO.getCompanyPhone1());//电话一
        baseCompany.setCompanyPhone2(baseCompanyVO.getCompanyPhone2());//电话二
        baseCompany.setShipsCount(baseCompanyVO.getShipsCount());//船舶总数
        baseCompany.setCarsCount(baseCompanyVO.getCarsCount());//车辆总数
        if(baseCompanyVO.getLogoImage()!=null&&!baseCompanyVO.getLogoImage().equals("")) {//如果不为空，则执行
            baseCompany.setLogoImage(baseCompanyVO.getLogoImage());//公司图标和Logo图标
        }
        baseCompany.setComments(baseCompanyVO.getComments());//备注

        baseCompanyService.update(baseCompany);//编辑公司

    }

    @Override
    @Transactional//事务回滚
    public Object companyFindOne(String id) {
        BaseCompany baseCompany = baseCompanyService.view(id);
        BaseCompanyUser baseCompanyUser = new BaseCompanyUser();
        baseCompanyUser.setBaseCompany(baseCompany);
        return baseCompanyUserService.findOne(baseCompanyUser);
    }
}
